﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.Mvc;

namespace HitchHikersGuideToTheGalaxy.Models
{
    [Bind(Include = "Id,GradeId,UserInfoSSNGive,UserInfoSSNRecieve,Note")]
    public partial class Rating
    {
        public bool IsValid
        {
            get { return (GetRuleViolations().Count() == 0); }
        }

        public IEnumerable<RuleViolation> GetRuleViolations()
        {
            if (String.IsNullOrEmpty(Id.ToString()))
                yield return new RuleViolation("Id is required", "Id");

            if (String.IsNullOrEmpty(GradeId.ToString()))
                yield return new RuleViolation("GradeId is required", "GradeId");

            if (String.IsNullOrEmpty(UserInfoSSNGive))
                yield return new RuleViolation("UserInfoSSNGive is required", "UserInfoSSNGive");

            if (String.IsNullOrEmpty(UserInfoSSNRecieve)) // TODO: laga nafn í grunni og elta svo út í model/view/controller
                yield return new RuleViolation("UserInfoSSNRecive is required", "UserInfoSSNRecieve");

            if (String.IsNullOrEmpty(Note))
                yield return new RuleViolation("Note is required", "Note");

            yield break;
        }

        partial void OnValidate(ChangeAction action)
        {
            if (!IsValid)
                throw new ApplicationException("Rule violations prevent saving");
        }
    }
}
